﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crosland.Automotriz.Core;
using Crosland.Automotriz.Entidad;

namespace Crosland.Automotriz.Data.Dao.Linq
{
    public class clsTD_MarcaDAO
    {
        private AutomotrizEntities ContextoDeObjs = new AutomotrizEntities();
    

        public bool Insertar(Crosland.Automotriz.Core.TD_MARCA Obj)
        {
            try
            {
                ContextoDeObjs.AddObject("TD_MARCA", Obj);
                ContextoDeObjs.SaveChanges();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
        public bool Modificar(Crosland.Automotriz.Core.TD_MARCA obj)
        {
            try
            {
                ContextoDeObjs.AttachTo("TD_MARCA", obj);
                ContextoDeObjs.ObjectStateManager.ChangeObjectState(obj, System.Data.EntityState.Modified);
                ContextoDeObjs.SaveChanges();
            }
            catch (Exception)
            {
                return false;

            }
            return true;
        }
        public bool Eliminar(Crosland.Automotriz.Core.TD_MARCA obj)
        {
            try
            {
                ContextoDeObjs.AttachTo("TD_MARCA", obj);
                ContextoDeObjs.DeleteObject(obj);
                ContextoDeObjs.SaveChanges();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
        public List<Crosland.Automotriz.Core.TD_MARCA> listar()
        {

            List<Crosland.Automotriz.Core.TD_MARCA> lista = new List<Crosland.Automotriz.Core.TD_MARCA>();
            try
            {
                var q = from c in ContextoDeObjs.TD_MARCA
                        select c;
                lista = q.ToList();

            }
            catch (Exception)
            {
            }
            return lista;
        }
        public List<Crosland.Automotriz.Entidad.TD_MARCA> ListarMarcas()
        {
         DM_CROSLAND_20130417Entities ContextoDeObjsEntidad = new DM_CROSLAND_20130417Entities();
            List<Crosland.Automotriz.Entidad.TD_MARCA> lista = new List<Crosland.Automotriz.Entidad.TD_MARCA>();
            try
            {
                var q = from c in ContextoDeObjsEntidad.TD_MARCA
                        where c.IND_AUTOMOTRIZ == 1
                        select c;
                lista = q.ToList();
            }
            catch (Exception)
            {
            }
            return lista;
        }
        public Crosland.Automotriz.Entidad.TD_MARCA ObtenerMarca(int ID_MARCA)
        {
            DM_CROSLAND_20130417Entities ContextoDeObjsEntidad = new DM_CROSLAND_20130417Entities();
            Crosland.Automotriz.Entidad.TD_MARCA u = new Entidad.TD_MARCA();
            try
            {
                var results = from c in ContextoDeObjsEntidad.TD_MARCA
                              where c.ID_MARCA == ID_MARCA
                              select c;


                foreach (var myEntity in results.Distinct().ToArray())
                {
                    u.DES_LOGO = myEntity.DES_LOGO;
                }
            }
            catch (Exception)
            {

            }
            return u;
        }
        public List<Crosland.Automotriz.Core.TD_MARCA> listar_marca_activos()
        {

            List<Crosland.Automotriz.Core.TD_MARCA> lista = new List<Crosland.Automotriz.Core.TD_MARCA>();
            try
            {
                var q = from c in ContextoDeObjs.TD_MARCA
                        where c.IND_AUTOMOTRIZ == 1
                        orderby c.DES_MARCA
                        select c;
                lista = q.ToList();

            }
            catch (Exception)
            {
            }
            return lista;
        }

        public IEnumerable<Object> listaMarcaActivoxRueda(String strNroRueda, String strvOperacion)
        {
            IEnumerable<Object> lista = new List<Object>();
            try
            {
                var q = (from tmo in ContextoDeObjs.TD_MODELO
                         join tma in ContextoDeObjs.TD_MARCA on tmo.TD_MARCA.ID_MARCA equals tma.ID_MARCA
                         orderby tma.DES_MARCA
                         where
                           tmo.IND_BAJA == 0 &&
                           tmo.TIP_NUM_RUEDA == strNroRueda
                         select new
                         {
                             tma.ID_MARCA,
                             tma.COD_MARCA,
                             tma.DES_MARCA,
                             tma.DES_MARCA_ABRE,
                             tma.IND_GRUPOCROSLAND,
                             tma.IND_AUTOMOTRIZ,
                             tma.DES_LOGO
                         }).Distinct();

                if (strvOperacion == "C") q = q.Where(f1 => f1.IND_AUTOMOTRIZ == 1);
                lista = q.OrderBy(x=>x.DES_MARCA).ToList();
            }
            catch (Exception)
            {
                lista = null;
            }
            return lista;
        }
    }
}
